package com.vercon.moensmart.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.vercon.common.Constants;
import com.vercon.common.utils.LogUtils;
import com.vercon.common.utils.MirrorDataStorage;
import com.vercon.moensmart.mode.controller.ShowerHeadDevice;
import com.vercon.moensmart.mode.controller.ToiletDevice;
import com.vercon.moensmart.mode.controller.TowelRackDevice;

import java.util.ArrayList;
import java.util.List;

public class DeviceControllerViewModel extends ViewModel {


    public MutableLiveData<ToiletDevice>  toiletLiveData;
    public MutableLiveData<ArrayList<TowelRackDevice>> towelRackListLiveData;
    public MutableLiveData<ShowerHeadDevice> showerHeadLiveData;

    private Gson mGson = new Gson();

    public DeviceControllerViewModel(){
        toiletLiveData = new MutableLiveData<>();
        towelRackListLiveData = new MutableLiveData<>();
        showerHeadLiveData = new MutableLiveData<>();
        towelRackListLiveData.setValue(new ArrayList<>());
    }

    public LiveData<ToiletDevice> getToiletLiveData(){
        return toiletLiveData;
    }

    public void setTowelRackLiveData(String deviceId){
        boolean isHad = false;
        TowelRackDevice AddDevice = new TowelRackDevice(deviceId);
        ArrayList<TowelRackDevice> localDevices = new ArrayList<>();
        String localData = MirrorDataStorage.instance().getString(Constants.LOCAL_TOWEL_RACK_LIST_DATA,"");
        if(!localData.equals("")){
            try {
                LogUtils.e("DeviceController","setTowelRackLiveData : " + localData);
                localDevices = mGson.fromJson(localData,new TypeToken<ArrayList<TowelRackDevice>>(){}.getType());
                if(localDevices != null ) {
                    for (int i = 0; i < localDevices.size(); i++) {
                        if(localDevices.get(i).device_id.equals(deviceId)){
                            isHad = true;
                            //AddDevice = localDevices.get(i);
                        }
                    }
                }
            }catch (Exception e){ e.printStackTrace(); }
        }
        if(!isHad){
            localDevices.add(AddDevice);
        }
        towelRackListLiveData.postValue(localDevices);
        saveLocalTowelRackList(localDevices);
    }

    /**
     * 清空绑定的毛巾架 设备状态
     */
    public void clearTowelRackList(){
        towelRackListLiveData.postValue(new ArrayList<>());
    }

    /**
     * 获取所有毛巾架当前的设备状态
     * @return
     */
    public LiveData<ArrayList<TowelRackDevice>> getTowelRackLiveData(){
        return towelRackListLiveData;
    }

    /**
     * 设置毛巾架的设备状态 : 开关
     * @param deviceId
     * @param powerSwitch
     */
    public void setTowelRack_PowerSwitch(String deviceId,int powerSwitch){
        ArrayList<TowelRackDevice> devices = towelRackListLiveData.getValue();
        for (int i = 0; i < devices.size(); i++) {
            if(devices.get(i).device_id.equals(deviceId)){
                devices.get(i).power_switch = powerSwitch;
                towelRackListLiveData.postValue(devices);
            }
        }
        saveLocalTowelRackList(devices);
    }

    /**
     * 设置毛巾架的设备状态 : 温度
     * @param deviceId
     * @param temperature
     */
    public void setTowelRack_Temperature(String deviceId,int temperature){
        ArrayList<TowelRackDevice> devices = towelRackListLiveData.getValue();
        for (int i = 0; i < devices.size(); i++) {
            if(devices.get(i).device_id.equals(deviceId)){
                devices.get(i).temperature = temperature;
                towelRackListLiveData.postValue(devices);
            }
        }
        saveLocalTowelRackList(devices);
    }

    /**
     * 设置毛巾架的设备状态 : 定时
     * @param deviceId
     * @param time
     */
    public void setTowelRack_Time(String deviceId,int time){
        ArrayList<TowelRackDevice> devices = towelRackListLiveData.getValue();
        for (int i = 0; i < devices.size(); i++) {
            if(devices.get(i).device_id.equals(deviceId)){
                devices.get(i).time = time;
                towelRackListLiveData.postValue(devices);
            }
        }
        saveLocalTowelRackList(devices);
    }

    private synchronized void saveLocalTowelRackList(ArrayList<TowelRackDevice> devices){
        MirrorDataStorage.instance().storeString(Constants.LOCAL_TOWEL_RACK_LIST_DATA,mGson.toJson(devices));
    }

    public LiveData<ShowerHeadDevice> getShowerHeadLiveData(){
        return showerHeadLiveData;
    }



}

